﻿using MasterMasterMode.Common.Systems;
using MasterMasterMode.Content.Biomes;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace MasterMasterMode.Content.NPCs
{
    public class GoldfishWarrior : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[Type] = Main.npcFrameCount[NPCID.GoldfishWalker];
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Times.DayTime,
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.Surface,
                new FlavorTextBestiaryInfoElement("Mods.MasterMasterMode.Bestiary.GoldfishWarrior"),
            ]);
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (spawnInfo.Player.ZoneOverworldHeight && InvasionSystem.ArmyofwarriorgoldfishesUp && Main.invasionX == Main.spawnTileX)
            {
                return 100f;
            }
            return 0f;
        }
        public override bool CheckDead()
        {
            if (InvasionSystem.ArmyofwarriorgoldfishesUp)
            {
                Main.invasionSize--;
            }
            return true;
        }

        public override void SetDefaults()
        {
            NPC.width = 24;
            NPC.height = 44;
            NPC.damage = 20;
            NPC.defense = 8;
            NPC.lifeMax = 30;
            NPC.HitSound = SoundID.NPCHit1;
            NPC.DeathSound = SoundID.NPCDeath1;
            NPC.value = 50f;
            NPC.aiStyle = 3;
            AIType = 524;
            AnimationType = NPCID.GoldfishWalker;
            NPC.knockBackResist = 0.5f;
            NPC.noTileCollide = false;
            NPC.noGravity = false;
            SpawnModBiomes = [ModContent.GetInstance<Armyofwarriorgoldfishes>().Type];
            Music = MusicID.OldOnesArmy;
        }

        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Water, 2.5f * hit.HitDirection, -2.5f, 0, default, 1.2f);
                }
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, 553);
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, 554);
            }
            else
            {
                for (int i = 0; i < 3; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Water, hit.HitDirection, -1f, 0, default, 0.8f);
                }
                if (Main.rand.NextBool(5))
                {
                    if (Main.netMode != NetmodeID.MultiplayerClient)
                    {
                        int enlarged = NPC.NewNPC(NPC.GetSource_FromThis(), (int)NPC.Center.X, (int)NPC.Center.Y, ModContent.NPCType<EnlargedGoldfishWarrior>());
                        Main.npc[enlarged].velocity = NPC.velocity;
                        for (int i = 0; i < 15; i++)
                        {
                            Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.YellowTorch, 0f, 0f, 0, default, 2f);
                        }
                        SoundStyle Sound = new($"{nameof(MasterMasterMode)}/Assets/Sounds/Bigger");
                        SoundEngine.PlaySound(Sound, NPC.Center);
                        NPC.active = false;
                    }
                }
            }
        }
    }
}